package string


/**
字符串前缀是否是prefix 是为true
 */
func HasPrefix(s, prefix string) bool  {
	return len(s)>=len(prefix) && s[0:len(prefix)]==prefix
}


/**
字符串后缀是否为suffix 是为true
 */
func HasSuffix(s, suffix string) bool  {
	return len(s) >= len(suffix) && s[len(s)-len(suffix):]==suffix
}


/**
从行首截取字符串
 */
func SubPrefix(s string, len int) string  {
	return string(s[len:])
}

//首字母大写
func UpperFirst(str string) string {
	if len(str) < 1 || str[0] < 'a' || str[0] > 'z' {
		return str
	}

	strB := []byte(str)
	strB[0] = strB[0] - byte(32)
	return string(strB)
}

//首字母小写
func LowerFirst(str string) string {
	if len(str) < 1 || str[0] < 'A' || str[0] > 'Z' {
		return str
	}

	strB := []byte(str)
	strB[0] = strB[0] + byte(32)
	return string(strB)
}

//下划线变为驼峰
func UnderlineToCamel(str string)string{
	strPortions := strings.Split(str, "_")
	for i := range strPortions{
		strB := []byte(strPortions[i])
		if strB[0] >= 'a' && strB[0] <= 'z'{
			strB[0] = strB[0] - byte(32)
			strPortions[i] = string(strB)
		}
	}
	return strings.Join(strPortions, "")
}

//驼峰变为下划线
func CamelToUnderline(str string)string{
	strB := []byte{}
	strLen := len(str)
	for i := 0; i < strLen; i++ {
		if str[i] >= 'A' && str[i] <= 'Z' {
			if i > 0 {
				strB = append(strB, '_')
			}
			strB = append(strB, str[i] + byte(32))
		} else {
			strB = append(strB, str[i])
		}
	}
	return string(strB)
}

//base64 编码
func Base64Encode(value string) string {
	encode := base64.StdEncoding.EncodeToString([]byte(value))
	return encode
}

//base64解码
func Base64Decde(value string) ([]byte, error) {
	decode, err := base64.StdEncoding.DecodeString(value)
	return decode, err
}